package game

import (
	"encoding/json"
	"fmt"
	"gosum/server/src/csv"
	"io/ioutil"
	"os"
	"time"
)

type RoleInfo struct {
	RoleId   int
	GetTimes int
	//todo 等级 经验 装备
	RelicsInfo []int // 用slice存五个位置的信息 0，0，1，0，0
	WeaponInfo int
}

type ModRole struct {
	RoleInfo  map[int]*RoleInfo
	HpPool    int
	HpCalTime int64

	player *Player
	path   string
}

func (self *ModRole) IsHasRole(roleId int) bool {
	return true
}

func (self *ModRole) GetRoleLevel(roleId int) int {
	return 80
}

func (self *ModRole) AddItem(roleId int, num int64) {
	config := csv.GetRoleConfig(roleId)
	if config == nil {
		fmt.Println("配置内容不存在", roleId)
		return
	}
	for i := 0; i < int(num); i++ {
		_, ok := self.RoleInfo[roleId]
		if !ok {
			data := new(RoleInfo)
			data.RoleId = roleId
			data.GetTimes = 1
			self.RoleInfo[roleId] = data
			fmt.Println("获得角色次数", roleId, "-----", data.GetTimes, "次")
		} else {
			//判断实际获得的东西

			self.RoleInfo[roleId].GetTimes++
			if self.RoleInfo[roleId].GetTimes >= csv.ADD_ROLE_TIME_NORMAL_MIN &&
				self.RoleInfo[roleId].GetTimes <= csv.ADD_ROLE_TIME_NORMAL_MAX {
				self.player.GetModBag().AddItemToBag(config.Stuff, config.StuffNum)
				self.player.GetModBag().AddItemToBag(config.StuffItem, config.StuffItemNum)

			} else {
				self.player.GetModBag().AddItemToBag(config.MaxStuffItem, config.MaxStuffItemNum)
			}
		}
	}
	itemConfig := csv.GetItemConfig(roleId)
	if itemConfig != nil {
		fmt.Println("获得角色:", itemConfig.ItemName, "获得角色次数", roleId, "-----", self.RoleInfo[roleId].GetTimes, "次")
	}
	self.player.GetModIcon().CheckGetIcon(roleId)

	//todo 好感度默认为10,获得名片
	self.player.GetModCard().CheckGetICard(roleId, 10)
}

func (self *ModRole) HandleSendRoleInfo(player *Player) {
	fmt.Println(fmt.Sprintf("当前拥有角色信息如下:"))
	for _, v := range self.RoleInfo {
		v.SendRoleInfo()
	}
}

func (self *ModRole) GetRoleInfoForPoolCheck() (map[int]int, map[int]int) {
	fiveInfo := make(map[int]int)
	fourInfo := make(map[int]int)

	for _, v := range self.RoleInfo {
		roleConfig := csv.GetRoleConfig(v.RoleId)
		if roleConfig == nil {
			continue
		}
		if roleConfig.Star == 5 {
			fiveInfo[roleConfig.RoleId] = v.GetTimes
		} else if roleConfig.Star == 4 {
			fourInfo[roleConfig.RoleId] = v.GetTimes
		}
	}

	return fiveInfo, fourInfo
}

func (self *RoleInfo) SendRoleInfo() {
	fmt.Println(fmt.Sprintf("%s:,累计获得次数:%d", csv.GetItemName(self.RoleId), self.GetTimes))
}

//回复血量

func (self *ModRole) CalHpPool() {
	if self.HpCalTime == 0 {
		self.HpCalTime = time.Now().Unix()
	}
	calTime := time.Now().Unix() - self.HpCalTime
	self.HpPool += int(calTime) * 10
	self.HpCalTime = time.Now().Unix()
	fmt.Println("当前回复血量", self.HpPool)
}

//圣遗物穿戴

func (self *ModRole) WearRelics(roleInfo *RoleInfo, relics *Relics, player *Player) {
	relicsConfig := csv.GetRelicsConfig(relics.RelicsId)
	if relicsConfig == nil {
		return
	}
	self.checkRelicsPos(roleInfo, relicsConfig.Pos)
	if relicsConfig.Pos < 0 || relicsConfig.Pos > len(roleInfo.RelicsInfo) {
		return
	}

	//替换圣遗物
	oldRelicsKeyId := roleInfo.RelicsInfo[relicsConfig.Pos-1]
	if oldRelicsKeyId > 0 {
		//判断位置上是否有圣遗物
		oddRelics := player.GetModRelics().RelicsInfo[oldRelicsKeyId]
		if oddRelics != nil {
			//将圣遗物与角色解绑
			oddRelics.RoleId = 0
		}
		//将圣遗物与位置解绑
		roleInfo.RelicsInfo[relicsConfig.Pos-1] = 0
	}

	//解除角色和圣遗物的绑定
	oldRoleId := relics.RoleId
	if oldRoleId > 0 {
		oddRole := player.GetModRole().RoleInfo[oldRoleId]
		if oddRole != nil {
			oddRole.RelicsInfo[relicsConfig.Pos-1] = 0
		}
		relics.RoleId = 0
	}

	roleInfo.RelicsInfo[relicsConfig.Pos-1] = relics.KeyId
	relics.RoleId = roleInfo.RoleId

	//oddRelicsKeyId > 0 ||oddRoleId > 0 圣遗物取下装备到另一个角色
	if oldRelicsKeyId > 0 && oldRoleId > 0 {
		oldRelics := player.GetModRelics().RelicsInfo[oldRelicsKeyId]
		oldRole := player.GetModRole().RoleInfo[oldRoleId]
		if oldRelics != nil && oldRole != nil {
			self.WearRelics(oldRole, oldRelics, player)
		}
	}

	roleInfo.showInfo(player)
}

//脱装备

func (self *ModRole) TakeOffRelics(roleInfo *RoleInfo, relics *Relics, player *Player) {
	relicsConfig := csv.GetRelicsConfig(relics.RelicsId)
	if relicsConfig == nil {
		return
	}
	self.checkRelicsPos(roleInfo, relicsConfig.Pos)
	if relicsConfig.Pos < 0 || relicsConfig.Pos > len(roleInfo.RelicsInfo) {
		return
	}
	if roleInfo.RelicsInfo[relicsConfig.Pos-1] != relics.KeyId {
		fmt.Println(fmt.Sprintf("当前角色没有穿戴这个物品"))
		return
	}
	roleInfo.RelicsInfo[relicsConfig.Pos-1] = 0
	relics.RoleId = 0

	roleInfo.showInfo(player)
}

//RelicsInfo []int 用slice存五个位置的信息 0，0，1，0，0
func (self *ModRole) checkRelicsPos(roleInfo *RoleInfo, pos int) {
	newSize := len(roleInfo.RelicsInfo)
	newAdd := pos - newSize
	//slice长度不够
	for i := 0; i < newAdd; i++ {
		roleInfo.RelicsInfo = append(roleInfo.RelicsInfo, 0)
	}
}

func (self *RoleInfo) showInfo(player *Player) {
	fmt.Println("当前角色Id", csv.GetItemName(self.RoleId), "当前角色Id", self.RoleId)

	weaponNow := player.GetModWeapon().WeaponInfo[self.WeaponInfo]
	if weaponNow == nil {
		fmt.Println(fmt.Sprintf("武器:未穿戴"))
	} else {
		fmt.Println(fmt.Sprintf("武器:%s,key:%d", csv.GetItemName(weaponNow.WeaponId), self.WeaponInfo))
	}

	suitMap := make(map[int]int)
	for _, v := range self.RelicsInfo {
		relicsConfig := player.GetModRelics().RelicsInfo[v]
		if relicsConfig == nil {
			fmt.Println("未穿戴圣遗物")
			continue
		}
		fmt.Println(fmt.Sprintf("%s,key:%d", csv.GetItemName(relicsConfig.RelicsId), v))

		relicsNowConfig := csv.GetRelicsConfig(relicsConfig.RelicsId)
		if relicsNowConfig != nil {
			suitMap[relicsNowConfig.Type]++
		}
	}
	suitSkill := make([]int, 0)
	for suit, num := range suitMap {
		for _, config := range csv.ConfigRelicsSuitMap[suit] {
			if num >= config.Num {
				suitSkill = append(suitSkill, config.SuitSkill)
			}
		}
	}
	for _, v := range suitSkill {
		fmt.Println("激活套装效果:", v)
	}
}

func (self *ModRole) WearWeapon(roleInfo *RoleInfo, weapon *Weapon, player *Player) {
	weaponConfig := csv.GetWeaponConfig(weapon.WeaponId)
	if weaponConfig == nil {
		fmt.Println("数据异常，武器配置不存在")
		return
	}

	roleConfig := csv.GetRoleConfig(roleInfo.RoleId)
	if roleConfig == nil {
		return
	}
	if roleConfig.Type != weaponConfig.Type {
		fmt.Println("武器和角色不匹配")
		return
	}

	oldWeaponKeyId := 0
	//角色装备武器时
	if roleInfo.WeaponInfo > 0 {
		oldWeaponKeyId = roleInfo.WeaponInfo
		//将角色与武器解绑
		roleInfo.WeaponInfo = 0

		//武器与角色解绑
		oddWeapon := player.GetModWeapon().WeaponInfo[oldWeaponKeyId]
		if oddWeapon != nil {
			weapon.RoleId = 0
		}
	}

	//武器被另外的角色装备
	oldRoleId := 0
	if weapon.RoleId > 0 {
		oldRoleId = weapon.RoleId
		weapon.RoleId = 0

		oldRole := player.GetModRole().RoleInfo[oldRoleId]
		if oldRole != nil {
			oldRole.WeaponInfo = 0
		}
	}

	//角色武器相互绑定
	roleInfo.WeaponInfo = weapon.KeyId
	weapon.RoleId = roleInfo.RoleId

	if oldWeaponKeyId > 0 && oldRoleId > 0 {
		oldWeapon := player.GetModWeapon().WeaponInfo[oldWeaponKeyId]
		oldRole := player.GetModRole().RoleInfo[oldRoleId]
		if oldWeapon != nil && oldRole != nil {
			self.WearWeapon(oldRole, oldWeapon, player)
		}
	}
}

func (self *ModRole) TakeOffWeapon(roleInfo *RoleInfo, weapon *Weapon, player *Player) {
	weaponConfig := csv.GetWeaponConfig(weapon.WeaponId)
	if weaponConfig == nil {
		fmt.Println("数据异常，武器配置不存在")
		return
	}

	if roleInfo.WeaponInfo != weapon.KeyId {
		fmt.Println("角色没有装备这把武器")
		return
	}
	roleInfo.WeaponInfo = 0
	weapon.RoleId = 0
}
func (self *ModRole) SaveData() {
	content, err := json.Marshal(self)
	if err != nil {
		return
	}
	err = ioutil.WriteFile(self.path, content, os.ModePerm)
	if err != nil {
		return
	}
}

func (self *ModRole) LoadData(player *Player) {

	self.player = player
	self.path = self.player.localPath + "/role.json"

	configFile, err := ioutil.ReadFile(self.path)
	if err != nil {
		self.InitData()
		return
	}
	err = json.Unmarshal(configFile, &self)
	if err != nil {
		self.InitData()
		return
	}

	if self.RoleInfo == nil {
		self.RoleInfo = make(map[int]*RoleInfo)
	}
	return
}

func (self *ModRole) InitData() {
	if self.RoleInfo == nil {
		self.RoleInfo = make(map[int]*RoleInfo)
	}
}
